package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class RegisterPage extends Utils {
    public static String url = "http://8.155.1.153:8081/user/register";
    public RegisterPage() {
        super(url);
    }

    public void checkPageRight() throws IOException {
        driver.manage().window().maximize();
        //查看页面关键元素
        //检查标题，小标题是否加载成功
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.top___1x2PK > div.header___3Kw-q > a > span"));
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.top___1x2PK > div.desc___vFX8U"));
        //检查注册框是否加载成功
        driver.findElement(By.cssSelector("#name"));
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#rePassword"));
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button"));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());


    }

    //页面能否正常注册
    /*注册失败
    * 姓名为空，账号为空，密码为空，确认密码为空
    * 注册账号已存在
    * 密码长度少于6位
    * 注册时两次密码不一致*/

    public void RegisterFail() throws IOException {
        driver.findElement(By.cssSelector("#name")).sendKeys("");
        driver.findElement(By.cssSelector("#username")).sendKeys("");
        driver.findElement(By.cssSelector("#password")).sendKeys("");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        String actualMsg=driver.findElement(By.cssSelector("#name_help > div")).getText();
        String expectMsg = "用户名是必填项！";
        assert actualMsg.equals(expectMsg);

        String actualMsg1=driver.findElement(By.cssSelector("#username_help > div")).getText();
        String expectMsg1 = "登录账号不能为空";
        assert actualMsg1.equals(expectMsg1);

        String actualMsg2=driver.findElement(By.cssSelector("#password_help > div")).getText();
        String expectMsg2 = "密码是必填项！";
        assert actualMsg2.equals(expectMsg2);

        String actualMsg3=driver.findElement(By.cssSelector("#rePassword_help > div")).getText();
        String expectMsg3 = "请再次输入密码";
        assert actualMsg3.equals(expectMsg3);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    public void RegisterFail1() throws IOException {
        driver.findElement(By.cssSelector("#name")).sendKeys("LiLi");
        driver.findElement(By.cssSelector("#username")).sendKeys("LiLi");
        driver.findElement(By.cssSelector("#password")).sendKeys("111111");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("111111");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        String actualMsg=driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div")).getText();
        String expectMsg = "账号已存在";
        assert actualMsg.equals(expectMsg);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    public void RegisterFail2() throws InterruptedException, IOException {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

        WebElement ele=driver.findElement(By.cssSelector("#name"));
        WebElement ele1=driver.findElement(By.cssSelector("#username"));
        WebElement ele2=driver.findElement(By.cssSelector("#password"));
        WebElement ele3=driver.findElement(By.cssSelector("#rePassword"));

        Actions action=new Actions(driver);
        action.doubleClick(ele)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果

        action.doubleClick(ele1)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果

        action.doubleClick(ele2)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果

        action.doubleClick(ele3)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果


        driver.findElement(By.cssSelector("#name")).sendKeys("AAZAAXCQ");
        driver.findElement(By.cssSelector("#username")).sendKeys("AAZAAXCQ");
        driver.findElement(By.cssSelector("#password")).sendKeys("111");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("111");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        String actualMsg=driver.findElement(By.cssSelector("#password_help > div")).getText();
        String expectMsg = "密码至少6位";
        assert actualMsg.equals(expectMsg);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    public void RegisterFail3() throws IOException {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

        WebElement ele2=driver.findElement(By.cssSelector("#password"));
        WebElement ele3=driver.findElement(By.cssSelector("#rePassword"));

        Actions action=new Actions(driver);
        action.doubleClick(ele2)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果

        action.doubleClick(ele3)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果

        driver.findElement(By.cssSelector("#password")).sendKeys("111111");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        String actualMsg=driver.findElement(By.cssSelector("#rePassword_help > div")).getText();
        String expectMsg = "两次密码不一致!";
        assert actualMsg.equals(expectMsg);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    /*注册成功
    姓名不为空，账号不为空，密码超过六位，确认密码与密码一致，点击注册账号按钮。
    返回登录按钮
     * */
    public void RegisterSuc() throws IOException {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

        Actions action=new Actions(driver);
        WebElement ele3=driver.findElement(By.cssSelector("#rePassword"));
        action.doubleClick(ele3)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果

        driver.findElement(By.cssSelector("#rePassword")).sendKeys("111111");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div:nth-child(4) > a")).click();
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(6) > a")).click();
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

}
